package com.hlkj.baojin.jwt;

import com.hlkj.baojin.dao.IUserDao;
import com.hlkj.baojin.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * 用户验证方法
 *
 * @author lzx
 * Created on 2017/12/8 9:18.
 */
@CacheConfig(cacheNames = "userD")
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    static final Logger logger = LoggerFactory.getLogger(JwtUserDetailsServiceImpl.class);

    private final IUserDao userRepository;

    @Autowired
    public JwtUserDetailsServiceImpl(IUserDao userRepository) {
        this.userRepository = userRepository;
    }


    //    @Cacheable
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("=====================我进来取用户");
        User user = userRepository.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        } else {
            return JwtUserFactory.create(user);
        }
    }


}


